package com.masblearn;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TankFrame extends Frame {
    GameModel gm = GameModel.getInstance();

    static final int GAME_WIDTH = Integer.parseInt((String) PropertiesManager.get("gameWith"));
    static final int GAME_HEIGHT = Integer.parseInt((String) PropertiesManager.get("gameHeight"));

    public TankFrame() throws HeadlessException {
        this.setSize(GAME_WIDTH, GAME_HEIGHT);
        this.setResizable(false);
        this.setTitle("坦克大战——设计模式");
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
        //键盘监听器
        this.addKeyListener(new MyKeyListener());
    }

    @Override
    public void paint(Graphics g) {
        //把他交给GameModel
        gm.paint(g);
    }

    //双缓冲。
    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics offScreen = offScreenImage.getGraphics();
        Color color = offScreen.getColor();
        offScreen.setColor(Color.black);
        offScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        offScreen.setColor(color);
        paint(offScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    class MyKeyListener extends KeyAdapter {
        boolean BL_LEFT = false;
        boolean BL_UP = false;
        boolean BL_DOWN = false;
        boolean BL_RIGHT = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_UP:
                    BL_UP = true;
                    break;
                case KeyEvent.VK_DOWN:
                    BL_DOWN = true;
                    break;
                case KeyEvent.VK_LEFT:
                    BL_LEFT = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    BL_RIGHT = true;
                    break;
                default:
                    break;
            }
            //设置方向
            setMainTankDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            super.keyReleased(e);
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_UP:
                    BL_UP = false;
                    break;
                case KeyEvent.VK_DOWN:
                    BL_DOWN = false;
                    break;
                case KeyEvent.VK_LEFT:
                    BL_LEFT = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    BL_RIGHT = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    gm.getMainTank().fire(FourDirFireStrategy.getInstance());
                    break;
                default:
                    break;
            }

            setMainTankDir();
        }

        private void setMainTankDir() {
            gm.getMainTank().setMoving(true);
            if (!BL_UP && !BL_DOWN && !BL_RIGHT && !BL_LEFT) gm.getMainTank().setMoving(false);
            else if (BL_UP) gm.getMainTank().setDir(Dir.UP);
            if (BL_DOWN) gm.getMainTank().setDir(Dir.DOWN);
            if (BL_LEFT) gm.getMainTank().setDir(Dir.LEFT);
            if (BL_RIGHT) gm.getMainTank().setDir(Dir.RIGHT);
        }
    }
}
